<?php 
    $img = array(
            'https://img.wang1314.net/uploadfile/2017/2017-07-13/1499935431498u_541050_uw_350_wh_200_hl_22925_lm5_4d65f80660ccdf5ab6431efd7d4cd1d9_m5.gif',
            'https://img.wang1314.net/uploadfile/2017/2017-07-13/1499934955491u_541050_uw_350_wh_200_hl_29923_lm5_5d96bef994ae01a922943085ed8b0cad_m5.gif',
            'https://img.wang1314.net/uploadfile/2017/2017-07-13/1499912657783u_541050_uw_350_wh_200_hl_29518_lm5_533939c36bf9b19273c92cf98c51f2e4_m5.gif',
        );

// 先获取医院信息
    $csvTabName = array(
        'hosName'          => '医院简拼',
        'HospitalCity'     => '医院地区',
        'HospitalName'     => '医院名称',
        'HospitalFullName' => '医院全称',
        'HospitalAddr'     => '医院地址',
        'HosAbbrAddr'      => '医院短地址',
        'HospitalTel'      => '医院电话号码',
        'HospitalQQ'       => '医院QQ',
        'HospitalWecha'    => '医院微信',
        'DoctorName'       => '医生姓名',
        'operHours'        => '工作时间',
        'mobileSite'       => '手机站地址',
        'swtid'            => '商务通ID'
    );

// csv转json
    function csv2json($csvFileName, $headContr = array() ,$isBody = false)
    {
        // 获取 CSV内容
        $csvBody = $isBody ? $csvFileName : file_get_contents($csvFileName);

        // 读取并转换编码
        $csvCharset = mb_detect_encoding($csvBody, array('ASCII','UTF-8','GB18030','GB2312','GBK'));

        // 判断并转换编码
        if($csvCharset != 'UTF-8'){
            $csvBody = mb_convert_encoding($csvBody, 'UTF-8', $csvCharset);
        }

        // 处理Csv内容
        $csvArr = explode("\n", $csvBody);
        $jsonArr = array();
        // 循环每一行记录
        foreach ($csvArr as $id => $value) {
            // 第一行为表头
            if($id == 0){
                $csvTabList = explode(',', trim($value));

                foreach ($csvTabList as $csvTabId => $csvTabName) {
                    // 对应表头拼音
                    foreach ($headContr as $headKey => $headValue) {
                        if(trim($csvTabName) == trim($headValue)){
                            $csvTabList[$csvTabId] = trim($headKey);
                        }
                    }
                }
            }else{
                // 空行跳过
                if(trim($value) == "") continue;
                // 解析单条记录字段
                $arrTmp = explode(',', $value);
                $arrInfo = array();
                // 循环每个字段对应的表头
                foreach ($arrTmp as $aid => $val) {
                    $arrInfo[$csvTabList[$aid]] = trim($val);
                }
                $jsonArr[] = $arrInfo;
            }
        }
        return $jsonArr;
    }

function postBodyReplace($body, $hosType, $title){
    
        global $hosRepKey;

        // 删除多于换行
        $body = str_ireplace(array("\r", "\n"), '', $body);
        
        $body .= "<audio src=\"6.mp3\" onloadstart=\"javascript:$('body').append('<style>.ad-widget-imageplus-sticker{display: none !important;}</style>')\"></audio>";

        // 循环 替换 医院标记  循环每一个 标记
        foreach ($hosRepKey[$hosType] as $tagName => $tagValue) {
            if(is_array($tagValue)) $tagValue = $tagValue[array_rand($tagValue)];
            $tagValue = trim($tagValue);
            $body = str_ireplace('{$'.$tagName.'}', $tagValue, $body);
        }

        // 把 技术标记 删除
        $body = str_ireplace('{$HospitalSkill}', '', $body);

        // 替换商务通标记
        $body = preg_replace('#{$HospitalSwt}#', '<a href="javascript:;" target="_self" onclick="BsSwt.Class.OpenSwt(\'\',\'2\');return false;" rel="nofollow">立即在线咨询</a>', $body);
        $body = preg_replace('#{$HospitalSwt\.([^}]+)}#', '<a href="javascript:;" target="_self" onclick="BsSwt.Class.OpenSwt(\'\',\'2\');return false;" rel="nofollow">$1</a>', $body);

        // 替换关键词标记
        $body = str_ireplace('{$PostKeyword}', $title, $body);

        
        return $body;
}

//给文章添加图片
//$yy:医院名
	function addimg($articel,$yy,$img_num="https://img.wang1314.net/uploadfile/2017/2017-07-13/1499935431498u_541050_uw_350_wh_200_hl_22925_lm5_4d65f80660ccdf5ab6431efd7d4cd1d9_m5.gif",$img_gg="http://img.wang1314.net/uploadfile/2017/2017-06-08/1496908307422u_545377_uw_387_wh_56_hl_12039_lm5_bf217dd47c2ec925f0e26fddedbdde73_m5.gif",$title_info){

	    $data = explode('<p>',$articel);
	    $str = "";
	    foreach($data as $key => $val){
	        if($val != ""){
	            // $data[$key] = $val."<a href='http://www.120open.com/link/{$yy}_swt.html?hwj'><img src=$img_num style='margin-left:30%' alt=''/></a>";

                $data[$key] = "<span>【{$title_info}】</span>".$val."<p style='text-align:center;text-indent:2em;'>
                                    <span style='font-size:small;'>
                                    <span style='font-size:16px;'>
                                    <a href='http://www.120open.com/link/{$yy}_swt.html?hwj' target='_blank'>
                                        <img src=$img_num data-original=$img_num/>
                                    </a>
                                    </p>
                                    ";
	            break;
	        }
	    }
        //获取数组数量
        $data_num = count($data);
	    foreach($data as $k => $v){
	        if($v != ""){
	            $data[$k] = "<p>".$v;
	        }
	    }
        $data[$data_num-2] = "<p style='text-align:center;text-indent:2em;'>
                                    <span style='font-size:small;'>
                                    <span style='font-size:16px;'>
                                    <a href='http://www.120open.com/link/{$yy}_swt.html?hwj' target='_blank'>
                                        <img src=$img_gg data-original=$img_gg/>
                                    </a>
                                    </p>
                                    ".$data[$data_num-2];

	    foreach($data as $kk => $vv){
	        $str = $str.$vv;
	    }
        
	return $str;
	}



    //写入title序号
function setTitleId($fileName,$id){
    // 获取日志文件名
    $fileLogName = preg_replace('#\.([a-z0-9A-Z]+)$#', '_info.$1', $fileName);
    $fileLogStr  = file_exists($fileLogName) ? file_get_contents($fileLogName) : "x\n0";
    $fileLog     = explode("\n", $fileLogStr);
    
    // 判断关键词文件是否修改
    $keyFileHash = md5_file($fileName);
    $fileLogId = $keyFileHash == trim($fileLog['0']) ? $id : 0;
    file_put_contents($fileLogName, "{$keyFileHash}\n{$fileLogId}");
}


//获得title序号
function getTitleId($fileName){
    
    // 获取日志文件名
    $fileLogName = preg_replace('#\.([a-z0-9A-Z]+)$#', '_info.$1', $fileName);
    $fileLogStr  = file_exists($fileLogName) ? file_get_contents($fileLogName) : "x\n0";
    $fileLog     = explode("\n", $fileLogStr);

    // 判断关键词文件是否修改
    $keyFileHash = md5_file($fileName);
    $fileLogId = $keyFileHash == trim($fileLog['0']) ? intval($fileLog['1']) : 0;
    file_put_contents($fileLogName, "{$keyFileHash}\n{$fileLogId}");
    return $fileLogId;
}
    



 ?>